<?php
namespace MyClass;
class Company extends \MyClass\Common {
    //入驻企业
    public function Show(){
      


        if($_REQUEST['city']) {
            $condition["company.CompanyRegionId"] = $_REQUEST['city'];
			$this->s('cityid',$_REQUEST['city']);
        }
		if($_REQUEST['cityname']){
				$this->s('cityname',$_REQUEST['cityname']);
		}
		if($_REQUEST['qytype']){
		
			if($_REQUEST['qytype'] ==1) {
				$condition["company.Caigou"] = 1;
			} elseif($_REQUEST['qytype'] ==2) {
				$condition["company.Gongying"] = 1;
			}
			$this->s('qytype',$_REQUEST['qytype']);
		}else {
            $condition["1"] = array("=1 and (company.Gongying= 1 or company.Caigou = 1)");
        }
        $condition["company.Status"] = 1;
       // $p = new \Extend\Page($this->m('company')->where($condition)->count(),15);
        $list = $this->m('company')
            ->where($condition)
            ->limit(6)
            ->select();
	
		if($_SESSION['UserInfo']['id']){
			if(!isset($_SESSION['UserInfo']['cid'])){
				$list = $this->m('company')
				->where($condition)
				->limit(6)
				->select();
			}else{
				$shcomp = $this->m("company")->field("Status")->where("Id=".$_SESSION['UserInfo']['cid'])->find();//查看企业是否已通过审核
				$this->s("shcomp",$shcomp);
				foreach($list as $key=>$vo){
					$bus = $this->m("business")->where("Cid=".$_SESSION['UserInfo']['cid']." AND Gid=".$vo['Id'])->find();
					if($bus){
						$list[$key]["ifgb"] = 2;//已关注
					}else{
						$list[$key]["ifgb"] = 1;//未关注
					}
				}
			}
		}

        $this->s('list',$list);

        $this->v();
	}
    //详情
    public function Info(){
        if(!$_REQUEST['Id']) {
            $this->error('参数错误!');
        }
        $search['Id'] = $_REQUEST['Id'];
        $info = $this->m('company')->where($search)->find();

	   //供应产品
	   $product = $this->m("products")->where("Cid=".$_REQUEST['Id'])->limit("3")->select();
		//采购信息
		$caigou = $this->m('bid')->where("Status > 0 AND Cid=".$_REQUEST['Id'])->field("Id,Title,Address,Co,Pubtime,Status,EndDate")->order("Pubtime desc")->limit("0,7")->select();//UNIX_TIMESTAMP(EndDate) 时间转换为时间戳
		//print_r($caigou);
        $this->s('company',$info);
        $this->s('caigou',$caigou);
        $this->s('product',$product);
        $this->v();
    }
	/*public function GzAjax(){
	
		$id = $_REQUEST['id'];
		$cid = $_SESSION['UserInfo']['cid'];
		if(!isset($_SESSION['UserInfo']['cid'])){
				$this->error("请完善公司信息信息");
		}
		$bus = $this->m("business")->where("Cid=".$cid." AND Gid=".$id)->select();
		if(count($bus)>0){
			echo 1;
		}else{
			echo 2;
		}
	}*/
	public function GzAjax(){
		$id = $_REQUEST['id'];
		$cid = $_SESSION['UserInfo']['cid'];
		if(!isset($_SESSION['UserInfo']['cid'])){
				$this->error("请完善公司信息信息");
		}
		if($id == $cid){
			echo 3;
			return false;
		}
		$bus = $this->m("business")->where("Cid=".$cid." AND Gid=".$id)->select();
		if(count($bus)>0){
			echo 1;
		}else{
			$busarr = array(
				'Cid'	=> $cid,
				'Gid'	=> $id,
				'BusinessTime'	=> date("Y-m-d H:i:s",time())
			
			);
			$res = $this->m("business")->add($busarr);
			if($res){
				echo 2;
			}else{
				echo 4;
			}
		}
		
	}

	//ajax预加载
	public function PageAjax(){
		if(!isset($_REQUEST['page'])){
				$this->error("非法操作");
		}
		$thispage = (int)$_REQUEST['page']*6;
		//$_REQUEST['page'] = 1;
		
        if($_REQUEST['city']) {
            $condition["company.CompanyRegionId"] = $_REQUEST['city'];
        }
		if($_REQUEST['qytype']){
			if($_REQUEST['qytype'] ==1) {
				$condition["company.Caigou"] = 1;
			} elseif($_REQUEST['qytype'] ==2) {
				$condition["company.Gongying"] = 1;
			}else{
				$condition["1"] = array("=1 and (company.Gongying= 1 or company.Caigou = 1)");
			}
		} else {
            $condition["1"] = array("=1 and (company.Gongying= 1 or company.Caigou = 1)");
        }
        $condition["company.Status"] = 1;
		$count = $this->m('company')->where($condition)->count();
        $list = $this->m('company')
            ->where($condition)
            ->limit($thispage.", 6")
            ->select();
		if($_SESSION['UserInfo']['id']){
			if(!isset($_SESSION['UserInfo']['cid'])){
				$this->error("请完善公司信息信息");
			}
			foreach($list as $key=>$vo){
				$bus = $this->m("business")->where("Cid=".$_SESSION['UserInfo']['cid']." AND Gid=".$vo['Id'])->find();
				if($bus){
					$list[$key]["ifgb"] = 2;//已关注
				}else{
					$list[$key]["ifgb"] = 1;//未关注
				}
			}
		}
		
		$citys = F("sys_arrcity");
		
		foreach($list as $key=>$v){
			
			foreach($citys as $vp){
				foreach($vp['cl'] as $vc){
					if($vc['id'] == $v['CompanyRegionId']){
						$list[$key]['thiscity']=$vp['name'].$vc['name'];
					}
					
				}	
			
			}
			
		}

		
		$ifhy = ceil($count/6);
		if((int)$_REQUEST['page']>$ifhy){
			$list['ifhy'] = 1;
		}
		echo json_encode($list);
	
	}

	
}